/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.ThreadAplicInterface;
import br.gov.cemaden.library.model.smp.ExecAplic;
import br.gov.cemaden.library.model.smp.ThreadAplic;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class ThreadAplicDao extends GenericDao implements ThreadAplicInterface {

    private Session session;

    public ThreadAplicDao(Session session) {
        this.session = session;
    }

    public List<ThreadAplic> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadAplic> threadAplic = null;
        try {
            Query query = session.getNamedQuery("ThreadAplic.findAll");
            threadAplic = (List<ThreadAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadAplicFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadAplic;
    }

    public ThreadAplic findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        ThreadAplic threadAplic = null;
        try {
            Query query = session.getNamedQuery("ThreadAplic.findById").setParameter("id", id);
            threadAplic = (ThreadAplic) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadAplicFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadAplic;
    }

    public List<ThreadAplic> findByLive(Integer live) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadAplic> threadAplic = null;
        try {
            Query query = session.getNamedQuery("ThreadAplic.findByLive").setParameter("live", live);
            threadAplic = (List<ThreadAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadAplicFacade.findByLive: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadAplic;
    }

    public List<ThreadAplic> findByPeak(Integer peak) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadAplic> threadAplic = null;
        try {
            Query query = session.getNamedQuery("ThreadAplic.findByPeak").setParameter("peak", peak);
            threadAplic = (List<ThreadAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadAplicFacade.findByPeak: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadAplic;
    }

    public List<ThreadAplic> findByTotal(Long total) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadAplic> threadAplic = null;
        try {
            Query query = session.getNamedQuery("ThreadAplic.findByTotal").setParameter("total", total);
            threadAplic = (List<ThreadAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadAplicFacade.findByTotal: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadAplic;
    }

    public List<ThreadAplic> findByExecAplic(ExecAplic execAplic) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<ThreadAplic> threadAplic = null;
        try {
            Query query = session.getNamedQuery("ThreadAplic.findByExecAplic").setParameter("execAplic", execAplic);
            threadAplic = (List<ThreadAplic>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no ThreadAplicFacade.findByExecAplic: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return threadAplic;
    }

    public ThreadAplic insert(ThreadAplic threadAplic) {
        try {
            saveOrUpdate(this.session, threadAplic);
        } catch (Exception ex) {
            Logger.getLogger(ThreadAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return threadAplic;
    }

    public ThreadAplic update(ThreadAplic threadAplic) {
        try {
            saveOrUpdate(this.session, threadAplic);
        } catch (Exception ex) {
            Logger.getLogger(ThreadAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return threadAplic;
    }

    public void delete(ThreadAplic threadAplic) {
        try {
            remove(this.session, threadAplic);
        } catch (Exception ex) {
            Logger.getLogger(ThreadAplicDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
